<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Agreement extends Common
{
    /**
     * User:Simony
     * Function: 协议表列表
     * @return mixed
     */
    public function agreementList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        //model
        $agreementModel = model('common/agreement/Agreement');

        //getData
        $list = $agreementModel->getList($where, 10,'*','sort desc');
        foreach ($list as &$v){
            $v['content'] = htmlspecialchars_decode( $v['content']);
        }
        //assign
        $this->assign('list', $list);
        $this->assign('page', $agreementModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 协议表添加修改
     */
    public function agreementAdd()
    {
        //params
        $id = input('param.id/d', '');

        //model
        $agreementModel = model('common/agreement/Agreement');
        $data = $agreementModel->getInfoByID($id);

        if ($this->request->isAjax()) {

            //post params
            $ajax_data = $this->request->post();

            $ajax_data['content'] =htmlspecialchars($ajax_data['content']);
            /*$validate = validate('Agreement');
            if (!$validate->check($ajax_data)) {
               return ['err' => $validate->getError()];
           }*/
            //judge
            if ($id && !$data) ds_json_encode('err', '数据不存在');


            if($id){
                $ajax_data['update_time']=time();
            }else{
                $ajax_data['create_time']=time();
            }

            $res = $id ? $agreementModel->edit(['id' => $id], $ajax_data) : $agreementModel->add($ajax_data);
            $res ? ds_json_encode('ret', '操作成功') : ds_json_encode('err', '操作失败');
        }
        isset($data['content']) && $data['content']= htmlspecialchars_decode($data['content']);

        $this->assign('data', $data);
        return $this->fetch();
    }

}